<?php

namespace App\Services\Ocean;

use App\Common\Helpers\Functions;
use App\Common\Tools\CustomException;
use App\Common\Enums\MaterialTypeEnums;
use App\Models\Ocean\OceanPromotionModel;
use App\Models\Ocean\OceanImageModel;
use App\Models\Ocean\OceanMaterialPromotionModel;
use App\Models\Ocean\OceanVideoModel;
use Illuminate\Support\Facades\DB;

class OceanMaterialPromotionService extends OceanService
{
    /**
     * OceanMaterialService constructor.
     * @param string $appId
     */
    public function __construct($appId = ''){
        parent::__construct($appId);
    }

    /**
     * @param array $option
     * @return bool
     * @throws CustomException
     * 同步
     */
    public function sync($option = []){
        $oceanPromotionModel = new OceanPromotionModel();
        if(!empty($option['date'])){
            $date = Functions::getDate($option['date']);
            $oceanPromotionModel = $oceanPromotionModel->whereBetween('promotion_modify_time', ["{$date} 00:00:00", "{$date} 23:59:59"]);
        }
        $oceanPromotions = $oceanPromotionModel->get();

        foreach($oceanPromotions as $oceanPromotion){
            if(!empty($oceanPromotion->extends->promotion_materials->video_material_list)){
                $materialType = MaterialTypeEnums::VIDEO;
                foreach($oceanPromotion->extends->promotion_materials->video_material_list as $videoMaterial){
                    $fileId = $videoMaterial->video_id;
                    $material = $this->getMaterial($materialType, $fileId);
                    if(!empty($material['material_id'])){
                        $this->save([
                            'material_id' => $material['material_id'] ?? '',
                            'promotion_id' => $oceanPromotion->id ?? '',
                            'material_type' => $materialType,
                            'material_status' => $videoMaterial->material_status,
                            'n8_material_id' => $material['n8_material_id'] ?? 0,
                            'signature' => $material['signature'] ?? '',
                        ]);
                    }
                }

            }elseif($oceanPromotion->extends->promotion_materials->image_material_list){
                $materialType = MaterialTypeEnums::IMAGE;
                foreach($oceanPromotion->extends->promotion_materials->image_material_list as $imageM){
                    foreach($imageM->images as $imageMaterial){
                        $fileId = $imageMaterial->image_id;
                        $material = $this->getMaterial($materialType, $fileId);
                        if(!empty($material['material_id'])){
                            $this->save([
                                'material_id' => $material['material_id'] ?? '',
                                'promotion_id' => $oceanPromotion->id ?? '',
                                'material_type' => $materialType,
                                'material_status' => $imageMaterial->material_status,
                                'n8_material_id' => $material['n8_material_id'] ?? 0,
                                'signature' => $material['signature'] ?? '',
                            ]);
                        }
                    }
                }
            }else{
                continue;
            }
        }

        return true;
    }

    /**
     * @param $materialType
     * @param $fileId
     * @return array|null
     * @throws CustomException
     * 获取素材
     */
    protected function getMaterial($materialType, $fileId){
        $material = null;
        if($materialType == MaterialTypeEnums::IMAGE){
            $oceanImage = OceanImageModel::find($fileId);
            if(empty($oceanImage)){
                return null;
            }

            $imageModel = new \App\Models\Material\ImageModel();
            $image = $imageModel->whereRaw("
                signature = '{$oceanImage->signature}'
            ")->first();

            $n8MaterialId = 0;
            if(!empty($image)){
                $n8MaterialId = $image->id;
            }

            $material = [
                'material_id' => $oceanImage->material_id,
                'n8_material_id' => $n8MaterialId,
                'signature' => $oceanImage->signature,
            ];
        }elseif($materialType == MaterialTypeEnums::VIDEO){
            $oceanVideo = OceanVideoModel::find($fileId);
            if(empty($oceanVideo)){
                return null;
            }

            $videoModel = new \App\Models\Material\VideoModel();
            $video = $videoModel->whereRaw("
                (signature = '{$oceanVideo->signature}' OR source_signature = '{$oceanVideo->signature}')
            ")->first();

            $n8MaterialId = 0;
            if(!empty($video)){
                $n8MaterialId = $video->id;
            }

            $material = [
                'material_id' => $oceanVideo->material_id,
                'n8_material_id' => $n8MaterialId,
                'signature' => $oceanVideo->signature,
            ];

        }else{
            throw new CustomException([
                'code' => 'UNKNOWN_MATERIAL_TYPE',
                'message' => '未知的素材类型',
            ]);
        }

        return $material;
    }

    /**
     * @param $item
     * @return bool
     * 保存
     */
    protected function save($item){
        $oceanMaterialPromotionModel = new OceanMaterialPromotionModel();
        $oceanMaterialPromotion = $oceanMaterialPromotionModel->where('material_id', $item['material_id'])
            ->where('promotion_id', $item['promotion_id'])
            ->first();

        if(empty($oceanMaterialPromotion)){
            $oceanMaterialPromotion = new OceanMaterialPromotionModel();
        }

        $oceanMaterialPromotion->material_id = $item['material_id'];
        $oceanMaterialPromotion->promotion_id = $item['promotion_id'];
        $oceanMaterialPromotion->material_type = $item['material_type'];
        $oceanMaterialPromotion->material_status = $item['material_status'];
        $oceanMaterialPromotion->n8_material_id = $item['n8_material_id'] ?? 0;
        $oceanMaterialPromotion->signature = $item['signature'] ?? '';
        $ret = $oceanMaterialPromotion->save();

        return $ret;
    }
}
